package com.zznzzn.androidshixun0826;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyListAdapter extends BaseAdapter {
    private ContactLib mcb = null;
    private Context mct = null;
    private LayoutInflater mlf = null;
    private int cur_index =-1;

    public MyListAdapter(Context ct, ContactLib cb ) {
        this.mcb = cb;
        this.mct = ct;
        this.mlf = LayoutInflater.from(ct);
    }

    static class Holder{
        public ImageView imageView;
        public TextView nameView,phoneView;
    }

    public void setSelected(int i){
        Log.d("===cur_index+==",String.valueOf(i));
        this.cur_index = i;
    }

    @Override
    public int getCount() {
        return mcb.getSize();
    }

    @Override
    public Object getItem(int position) {
        return mcb.getContact(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        Holder holder= null;
//        Contact mct = mcb.getContact(position);
//        if (convertView == null){
//            convertView = mlf.inflate(R.layout.list_item,null);
//            holder = new Holder();
//            holder.imageView = convertView.findViewById(R.id.item_img);
//            holder.nameView = convertView.findViewById(R.id.item_name);
//            holder.phoneView = convertView.findViewById(R.id.item_phone);
//            convertView.setTag(holder);
//        }else{
//            holder = (Holder) convertView.getTag();
//        }
//
//        //给控件赋值
//        holder.imageView.setImageBitmap(BitmapFactory.decodeByteArray(mct.getPhoto(),0,mct.getPhoto().length));
//        holder.nameView.setText(mct.getName());
//        holder.phoneView.setText(mct.getPhone());
//
//
//        return convertView;
        Holder holder= null;
        Contact mct = mcb.getContact(position);
        Log.d(String.valueOf(position), String.valueOf(cur_index));
        if (convertView == null){
            if (position == this.cur_index){
                convertView = mlf.inflate(R.layout.list_item_dark, null);
                holder = new Holder();
                holder.imageView = convertView.findViewById(R.id.item_img);
                holder.nameView = convertView.findViewById(R.id.item_name);
                holder.phoneView = convertView.findViewById(R.id.item_phone);
                convertView.setTag(holder);
            }else {
                convertView = mlf.inflate(R.layout.list_item, null);
                holder = new Holder();
                holder.imageView = convertView.findViewById(R.id.item_img);
                holder.nameView = convertView.findViewById(R.id.item_name);
                holder.phoneView = convertView.findViewById(R.id.item_phone);
                convertView.setTag(holder);
            }
        }else{
            if (position == this.cur_index){
                convertView = mlf.inflate(R.layout.list_item_dark,null);
                holder = new Holder();
                holder.imageView = convertView.findViewById(R.id.item_img);
                holder.nameView = convertView.findViewById(R.id.item_name);
                holder.phoneView = convertView.findViewById(R.id.item_phone);
                convertView.setTag(holder);
            }
            holder = (Holder) convertView.getTag();
        }

        //给控件赋值
        holder.imageView.setImageBitmap(BitmapFactory.decodeByteArray(mct.getPhoto(),0,mct.getPhoto().length));
        holder.nameView.setText(mct.getName());
        holder.phoneView.setText(mct.getPhone());


        return convertView;

    }
}
